<script type="text/javascript" src="includes/js/regExp.js"></script>
<?php
if (isset($_POST['loginS'])){
	$bd = openBD();
	// login unique
	$stC1 = $bd->prepare("SELECT count(*) FROM CLIENT WHERE login = :loginS LIMIT 1");
	$stC1->bindParam(':loginS', $_POST["loginS"]);
	$stC1->execute();
	$Data1 = $stC1->fetchColumn();
	// e-mail unique
	$stC2 = $bd->prepare("SELECT count(*) FROM CLIENT WHERE mail = :mail LIMIT 1");
	$stC2->bindParam(':mail', $_POST["mail"]);
	$stC2->execute();
	$Data2 = $stC2->fetchColumn();
	// deja signup
	if ($Data1 > 0 || $Data2 > 0) {
		?>
		<p class="ui-state-error">Login ou E-mail déjà utilisé.</p>
		<?php
	}
	// nouveau client
	else {
		require ('includes/regExp.php');
		if (validateMail($_POST["mail"]) && validateLength($_POST["loginS"]) && validateLength($_POST["mdpS"]) && validateL($_POST["nom"]) && validateL($_POST["prenom"])){
			try{
			$stS = $bd->prepare("INSERT INTO CLIENT(nom, prenom, login, password, mail) VALUES (:nom,:prenom,:loginS,unhex(sha1(:mdpS)),:mail)");
			$stS->bindParam(':loginS', $_POST["loginS"]);
			$stS->bindParam(':mdpS', $_POST["mdpS"]);
			$stS->bindParam(':nom', $_POST["nom"]);
			$stS->bindParam(':prenom', $_POST["prenom"]);
			$stS->bindParam(':mail', $_POST["mail"]);
			$stS->execute();
			} catch (PDOException $err) {
				echo "Error " . $err->getMessage();
			}
			// login automatique
			$stL = $bd->prepare("SELECT * FROM CLIENT WHERE login = :loginS AND password = unhex(sha(:mdpS)) LIMIT 1");
			$stL->bindParam(':loginS', $_POST["loginS"]);
			$stL->bindParam(':mdpS', $_POST["mdpS"]);
			$stL->execute();
			$result = $stL->fetchColumn();
			if ($result > 0) {
				?>
				<p class="ui-state-highlight">Vous vous êtes inscrit avec succès. Connexion au site ...</p>
				<?php
					$_SESSION["login"] = $_POST["loginS"];
				?>
				<meta http-equiv="refresh" content="2;">
				<?php
			}
		}
	}
}
?>
<script>
$(function() {
	$( "#dialog-form" ).dialog({autoOpen: false,modal: true, width: 450, height:400,resizable: false,
		buttons: {
			"Valider l'inscription": function() {
				$( "#infoClient").submit();
			},
			"Annuler": function() {
				$( this ).dialog( "close" );
			}
		}
	});
	$( "#signup-button" )
		.click(function() {
			$( "#dialog-form" ).dialog( "open" );
	});
});

</script>
<div id="dialog-form" title="Inscription">
	<p class="validateTips">Veuillez remplir le formulaire ci-dessus.</p>
 
	<form id="infoClient" name="infoClient" method="post" action="?p=singup" onsubmit="return checkEmpty(this)">
		<label for="login">
			Login : 
		</label>
		<input id="loginS" name="loginS" type="text" title="Six à quinze caractères"/>
		<label for="mdp">
			Mot de passe :
		</label>
		<input id="mdpS" name="mdpS" type="password" title="Six à quinze caractères"/>
		<label for="nom">
			Nom :
		</label>
		<input id="nom" name="nom" type="text" title="Deux à quinze caractères alphabétiques"/>
		<label for="prenom">
			Prénom :
		</label>
		<input id="prenom" name="prenom" type="text" title="Deux à quinze caractères alphabétiques"/>
		<label for="mail">
			E-mail :
		</label>
		<input id="mail" name="mail" type="text" title="Veuillez saisir un courriel valide"/>
	</form>
</div>